package cn.com.oceansoft.oauth.server.biz.controller;

import cn.com.oceansoft.oauth.server.biz.entity.OAuthClients;
import cn.com.oceansoft.oauth.server.biz.service.IOAuthClientsService;
import cn.com.oceansoft.oauth.server.biz.validator.OAuthClientsValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by BEN on 2016/4/19.
 */
@Controller
@RequestMapping("clients")
public class OAuthClientsController {

    @Autowired
    private IOAuthClientsService oauthService;

    @Autowired
    private OAuthClientsValidator oAuthClientsValidator;
     /*
      * Register client
      */
    @RequestMapping(value = "register", method = RequestMethod.GET)
    public String registerClient(Model model) {
        model.addAttribute("oAuthClients", new OAuthClients());
        return "clients/register_client";
    }


    /*
     * Submit register client
     */
    @RequestMapping(value = "register", method = RequestMethod.POST)
    public String submitRegisterClient(@ModelAttribute("oAuthClients") OAuthClients oAuthClients, BindingResult result) {
        oAuthClientsValidator.validate(oAuthClients, result);
        if (result.hasErrors()) {
            return "clients/register_client";
        }
        oauthService.registerClients(oAuthClients);
        return "redirect:/clients/manage";
    }


    /*
    * Register client
    */
    @RequestMapping(value = "manage", method = RequestMethod.GET)
    public String manageClient(Model model) {
        List<OAuthClients> clientsList= oauthService.loadAllOauthClients();
        model.addAttribute("clientsList", clientsList);
        return "clients/manage_client";
    }

    /*
    * Register client
    */
    @RequestMapping(value = "query", method = RequestMethod.GET)
    public String queryClient(Model model, @ModelAttribute("client_id") String client_id) {
        List<OAuthClients> clientsList= oauthService.loadLikeOauthClients(client_id+"%");
        model.addAttribute("clientsList", clientsList);
        return "clients/manage_client";
    }

    /*
       * Register client
       */
    @RequestMapping(value = "disable", method = RequestMethod.GET)
    public ModelAndView disable(Model model, @ModelAttribute("client_id") String client_id) {
        oauthService.unArchiveOauthClients(client_id);
        return null;
    }

    /*
      * Register client
      */
    @RequestMapping(value = "enable", method = RequestMethod.GET)
    public ModelAndView enable(Model model,@ModelAttribute("client_id") String client_id) {
        oauthService.archiveOauthClients(client_id);
        return null;
    }
}
